package net.infobank.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.oltu.oauth2.as.request.OAuthAuthzRequest;
import org.apache.oltu.oauth2.as.response.OAuthASResponse;
import org.apache.oltu.oauth2.as.response.OAuthASResponse.OAuthAuthorizationResponseBuilder;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;

/**
 * Servlet implementation class Authentication
 */
@WebServlet(description = "인증 관련 서블릿", urlPatterns = { "/Authentication" })
public class Authentication extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Authentication() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			OAuthAuthzRequest oauthRequest = new OAuthAuthzRequest(request);
			
//			validateRedirectionURI(outhRequest);
			
			int code = 1;
			String accessToken = StringUtils.defaultString( "" );
			String codeStr = StringUtils.defaultString( "" );
			String scope = "scope";
			
			OAuthResponse oauthRes = new OAuthAuthorizationResponseBuilder( request, code ).setAccessToken( accessToken )
				.setCode( codeStr )
				.setScope( scope ).buildBodyMessage();
			
			response.sendRedirect( oauthRes.getLocationUri() );
			
			
		} catch ( OAuthSystemException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch ( OAuthProblemException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
